package fa7.socialcode.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fa7.socialcode.dao.CategoriaDao;
import fa7.socialcode.dao.NoticiaDao;
import fa7.socialcode.model.Categoria;
import fa7.socialcode.model.Noticia;
import fa7.socialcode.service.NoticiaManager;

@Service(value = "noticiaManager")
public class NoticiaManagerImpl implements NoticiaManager {

	@Autowired
	private NoticiaDao noticiaDao;
	@Autowired
	private CategoriaDao categoriaDao;

	public void setNoticiaDao(NoticiaDao dao) {
		this.noticiaDao = dao;
	}

	public List findNoticias() {
		return noticiaDao.findNoticias();
	}
	
	public Noticia findNoticia(String noticiaId) {
		return noticiaDao.findNoticia(Long.valueOf(noticiaId));
	}
	
	public List findNoticiasByDate() {
		return noticiaDao.findNoticiasByDate();
	}
	
	public Noticia findNoticiasByDate(String noticiaId) {
		return noticiaDao.findNoticia(Long.valueOf(noticiaId));
	}

	public void saveNoticia(Noticia noticia, Long categoriaId) {

		if (noticia.getId() == 0) {
			noticia.setDataPostagem(new Date());
		} else {
			
			noticia.setDataPostagem(noticiaDao.findDataPostagemById(noticia
					.getId()));
		}

		Categoria cat = categoriaDao.findCategoria(categoriaId);
		noticia.setCategoria(cat);

		noticia.setUltimaAtualizacao(new Date());
		noticiaDao.saveNoticia(noticia);
	}

	public void removeNoticia(String noticiaId) {
		noticiaDao.removeNoticia(Long.valueOf(noticiaId));
	}

	public void setCategoriaDao(CategoriaDao categoriaDao) {
		this.categoriaDao = categoriaDao;
	}

	public List findCategorias() {
		return categoriaDao.findCategorias();
	}

}